﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ISS.IR.RRNMS.Library.DataAccess
{
    public static partial class Main
    {
        public static List<Item> SelectAllEditableCategories()
        {
            List<Item> LookupsCategories = new List<Item>();

            try
            {
                command.CommandText = "usp_SelectAllEditableCategories";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                DataTable dtEditableCategories = new DataTable();

                adapter.Fill(dtEditableCategories);

                for (int i = 0; i < dtEditableCategories.Rows.Count; i++)
                {
                    LookupsCategories.Add(
                        new Item()
                        {
                            Id = Convert.ToInt32(dtEditableCategories.Rows[i]["Id"].ToString()),
                            Value = dtEditableCategories.Rows[i]["Value"].ToString()
                        }); ;
                }
            }
            catch (Exception exc)
            { }

            return LookupsCategories;

        }


        public static List<Item> SelectCategoryItems(int CategoryId)
        {
            List<Item> CategoryItems = new List<Item>();

            try
            {
                command.CommandText = "usp_SelectCategoryItems";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Clear();

                command.Parameters.Add(new SqlParameter("CategoryId", CategoryId));

                DataTable dtCategoryItems = new DataTable();

                adapter.Fill(dtCategoryItems);

                for (int i = 0; i < dtCategoryItems.Rows.Count; i++)
                {
                    CategoryItems.Add(
                        new Item()
                        {
                            Id = Convert.ToInt32(dtCategoryItems.Rows[i]["Id"].ToString()),
                            Value = dtCategoryItems.Rows[i]["Value"].ToString()
                        }); ;
                }
            }
            catch (Exception exc)
            { }

            return CategoryItems;
        }

        public static void SaveLookup(int CategoryId, int ItemId, string LookupValue)
        {
            command.CommandText = "usp_SaveLookup";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("CategoryId", CategoryId));
            command.Parameters.Add(new SqlParameter("ItemId", ItemId));
            command.Parameters.Add(new SqlParameter("LookupValue", LookupValue));

            OpenConnection();
            command.ExecuteNonQuery();
            CloseConnection();
        }

        public static void DeleteLookup(int ItemId)
        {
            command.CommandText = "usp_DeleteLookup";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Clear();

            command.Parameters.Add(new SqlParameter("ItemId", ItemId));
            
            OpenConnection();
            command.ExecuteNonQuery();
            CloseConnection();
        }
    }
}
